﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppLogManage;
using Zocono.WCS.Application.AppPlcManage;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.PlcConnectPoolDomain;
using Zocono.WCS.Domain.ScGetWMSTaskDomain;
using Zocono.WCS.Infrastructure.ApllicationDto.WmsModel;

namespace Zocono.WCS.HangfireHost.Controllers
{
    /// <summary>
    /// 操作设备接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WmsInterfaceServiceController :  ControllerBase
    {
        private readonly LogApp _LogApp;
        private readonly PlcWriteBoMange _plcWriteBoMange;
        private readonly IPlcConnectionPoolService _plcConnectionPoolService;
        private readonly IScGetWMSTaskService _scGetWMSTaskService;
        private readonly WorkTaskInfoApp _workTaskInfoApp;

        public WmsInterfaceServiceController(IPlcConnectionPoolService plcConnectionPoolService,
            PlcWriteBoMange plcWriteBoMange,
            IScGetWMSTaskService scGetWMSTaskService
            , LogApp logApp, WorkTaskInfoApp workTaskInfoApp)
        {
            _plcConnectionPoolService = plcConnectionPoolService;
            _scGetWMSTaskService = scGetWMSTaskService;
            _plcWriteBoMange = plcWriteBoMange;
            _LogApp=logApp;
            _workTaskInfoApp = workTaskInfoApp;
        }


        /// <summary>
        /// 分容柜门监控
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> DoorOpenAlarmAsync(List<CapacityDoorOpen> capacityDoorOpen)
        {
            HeaderResult<bool> result = new();
            try
            {
                InterfaceLogInfo interfaceLogInfo = new InterfaceLogInfo();
                interfaceLogInfo.InterfacePara = $"{JsonConvert.SerializeObject(capacityDoorOpen)}";
                interfaceLogInfo.ActionName = "DoorOpenAlarmAsync";
                interfaceLogInfo.StartTime = DateTime.Now;
                interfaceLogInfo.InterfaceUrl = "DoorOpenAlarmAsync";
                interfaceLogInfo.Description = "类型：分容柜门状态";

                var res = await _plcWriteBoMange.DoorOpenAlarmAsync(capacityDoorOpen);
                if (res)
                {
                    result.IsSucceed = true;
                    result.Message = "成功！";
                }
                else
                {
                    result.IsSucceed = false;
                    result.Message = "失败！";

                }
                await _LogApp.AddInterfaceLogAsync(interfaceLogInfo);
            }
            catch (Exception e)
            {
                result.IsSucceed = false;
                result.Message = e.Message;
            }

            return result;
        }


        /// <summary>
        /// WMS手动解绑通知WCS手动完成任务
        /// </summary>
        /// <param name="unbandinWCS"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<HeaderResult<bool>> WMSManualUnbindingNotificationAsync(UnbandinWCS unbandinWCS)
        {
            HeaderResult<bool> result = new();
            try
            {
                InterfaceLogInfo interfaceLogInfo = new InterfaceLogInfo();
                interfaceLogInfo.InterfacePara = $"{JsonConvert.SerializeObject(unbandinWCS)}";
                interfaceLogInfo.ActionName = "WMSManualUnbindingNotificationAsync";
                interfaceLogInfo.StartTime = DateTime.Now;
                interfaceLogInfo.InterfaceUrl = "WMSManualUnbindingNotificationAsync";
                interfaceLogInfo.Description = "WMS手动解绑通知WCS手动完成任务";

                var res = await _workTaskInfoApp.WMSManualUnbindingNotificationAsync(unbandinWCS);

                result.IsSucceed = res.IsSucceed;
                result.Message = res.Message;

                await _LogApp.AddInterfaceLogAsync(interfaceLogInfo);
            }
            catch (Exception e)
            {
                result.IsSucceed = false;
                result.Message = e.Message;
            }

            return result;

        }


    }
}
